package com.cade.agriculturemall.view.personalCenter.discount.myDiscountFragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseFragment;
import com.cade.agriculturemall.model.Ticket;
import com.cade.agriculturemall.widget.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hshunxin on 2018/2/24.
 */

public class MyDiscountFragment extends BaseFragment<MyDiscountContract.IPresenter> implements MyDiscountContract.IView {
    private MyDiscountAdapter adapter;
    private RecyclerView mRecyclerView;
    private EmptyLayout emptyLayout;

    @Override
    public int setupLayout() {
        return R.layout.activity_my_discount_fragment;
    }

    @Override
    public void setupView() {
        mRecyclerView = (RecyclerView) getView().findViewById(R.id.my_discount_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        emptyLayout = (EmptyLayout) getView().findViewById(R.id.my_discount_emptylayout);
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void onResume() {
        super.onResume();
        emptyLayout.loading();
        mPresenter.downTicket();
    }


    @Override
    public void setupData() {
    }

    @Override
    public MyDiscountContract.IPresenter setupPresenter() {
        return new MyDiscountPresenter(this);
    }

    private List<MyDiscountViewHolder> models = new ArrayList<>();

    @Override
    public void getMyDiscount(List<Ticket> activeList, List<Ticket> noActiveList) {
        emptyLayout.reset();
        models.clear();
        if ((activeList.size() + noActiveList.size()) == 0) {
            emptyLayout.empty();
        } else {
            if (activeList.size() != 0) {
                for (Ticket ticket : activeList) {
                    models.add(new MyDiscountViewHolder(MyDiscountViewHolder.TYPE_ACTIVE, ticket));
                }
            }
            if (noActiveList.size() != 0) {
                models.add(new MyDiscountViewHolder(MyDiscountViewHolder.TYPE_LINE, null));
                for (Ticket ticket : noActiveList) {
                    models.add(new MyDiscountViewHolder(MyDiscountViewHolder.TYPE_NO_ACTIVE, ticket));
                }
            }
            adapter = new MyDiscountAdapter(getActivity(), models);
            mRecyclerView.setAdapter(adapter);
        }
    }
}
